import com.codezen.component.minibrowser.CzMiniBrowser;
import com.codezen.helper.Worker;

import flash.events.ErrorEvent;
import flash.events.Event;

import logic.SerialsDataLoader;

import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
import mx.core.FlexGlobals;
import mx.managers.PopUpManager;
import mx.utils.ObjectUtil;

import utils.anime.AniSerial;
import utils.serials.Episode;
import utils.serials.Serial;

private var dl:SerialsDataLoader;

// data providers
[Bindable]
private var lastEpisodes:ArrayCollection;
[Bindable]
private var savedEpisodes:ArrayCollection;
[Bindable]
private var seriesList:ArrayCollection;
[Bindable]
private var iviewSeriesList:ArrayCollection;
[Bindable]
private var episodesList:ArrayCollection;
[Bindable]
private var seasonsList:ArrayCollection;

// offline work flag
[Bindable]
private var isOffline:Boolean = false;

// seasons filter
private var showSeason:String = "1";

// old state
private var oldState:String;

// current episode
public var currentEpisode:Episode;

// current serial
public var currentSeries:Serial;

private function onLoginState():void{	
	// set login and pass
	login_input.text = login;
	pass_input.text = pass;
}

private function onMainState():void{
	// load saved
	if(dbInit && isOffline)
		dbLoadSaveEpisodes();
}


//
private function tryLogin(isOffline:Boolean = false):void{
	// load saved
	if(dbInit)
		dbLoadSaveEpisodes();
	// do login
	if(isOffline){		
		currentState = "Main";
		viewstack1.removeChild(new_eps_view);
		viewstack1.removeChild(iview_view);
		viewstack1.removeChild(series_view);
		viewstack1.removeChild(anime_view);
		//viewstack1.selectedChild = saved_eps_view;
	}else{
		loadSeriesList();
	}
}

private function loadSeriesList():void{
	// serials
	dl = new SerialsDataLoader(login, pass);
	dl.addEventListener(ErrorEvent.ERROR, onAuthError);
	dl.addEventListener(Event.INIT, onAuthLoad);
}

private function loadAnimeList():void{
	// animes
	an = new AnimeDataLoader(login, pass);
	an.addEventListener(ErrorEvent.ERROR, onAuthError);
	an.addEventListener(Event.INIT, onAniAuthLoad);
}

private function onAuthLoad(evt:Event):void{
	// remove event listener
	dl.removeEventListener(Event.INIT, onAuthLoad);
	
	// init
	lastEpisodes = new ArrayCollection();
	
	// get last eps
	var item:Episode;
	for each(item in dl.lastEpisodes){
		lastEpisodes.addItem(item);
	}
	
	// check new eps from DB
	dbLoadNewEpisodes();
	
	// clean
	item = null;
	
	// change state
	currentState = "Main";
	
	// get series
	dl.addEventListener(Event.COMPLETE, onDataLoad);
	dl.requestData();
}

private function onDataLoad(evt:Event):void{
	// remove event listener
	dl.removeEventListener(Event.COMPLETE, onDataLoad);
	
	// init
	seriesList = new ArrayCollection();
	
	// set series
	var item:Serial;
	for each(item in dl.seriesList){
		seriesList.addItem(item);
	}
	
	// create sort by names
	var sort:Sort = new Sort();
	sort.fields = [new SortField("title")];
	
	// sort
	seriesList.sort = sort;
	seriesList.refresh();
	
	// load series from DB
	dbLoadSeries();
	
	// clean
	item = null;
	sort = null;
	
	// force GC
	//startGCCycle();
	
	// load anime
	loadAnimeList();
}

private function loadSeries(id:String):void{
	dl.addEventListener(Event.COMPLETE, onSeriesLoad);
	dl.loadSeries(id);
}

private function onSeriesLoad(e:Event):void{
	dl.removeEventListener(Event.COMPLETE, onSeriesLoad);
	
	// init
	episodesList = new ArrayCollection();
	
	// get eps
	var item:Episode;
	for each(item in dl.episodesList){
		episodesList.addItem(item);
	}
	
	// clean
	item = null;
	
	// set seasons
	breakSeasons();
}

private function breakSeasons():void{
	seasonsList = new ArrayCollection();
	
	var tmp:Array = [];
	var item:Episode;
	for each(item in episodesList){
		if(tmp.lastIndexOf(item.season) < 0){
			tmp.push(item.season);
			seasonsList.addItem({label:"Сезон "+item.season,season:item.season});
		}
	}
	
	// clean
	item = null;
	tmp = null;
	
	// force GC
	startGCCycle();
	
	// set index
	seasons_combobox.selectedIndex = 0;
	filterSeasons(null);
}

private function onAuthError(evt:Event):void{
	// remove event listener
	dl.removeEventListener(ErrorEvent.ERROR, onAuthError);
}